/**
 * 添加article表数据
 * */ 

const db = require('./db')
const fs = require('fs')
const path = require('path')

// 读取文件的函数
function readFileFn(subject) {
  let mypath = path.join(__dirname, `../assets/${subject}.txt`)
  return new Promise((resolve, reject) => {
    fs.readFile(mypath, (err, data) => {
      if (err) throw err
      resolve(data.toString())
    })
  })
}

let vueContent, reactContent, angularContent
var fn = async () => {
  // 分别读取这几份txt文件
  vueContent = await readFileFn('vue')
  reactContent = await readFileFn('react')
  angularContent = await readFileFn('angular')

  let data = [
    { id: 0, title: "一套框架多种平台 移动端&桌面端", author: "杰哥", date: "2022-08-14", imgUrl: "/images/banner1.png", content: angularContent },
    { id: 1, title: "渐进式的JavaScript 框架", author: "发哥", date: "2022-08-13", imgUrl: "/images/banner2.png", content: vueContent },
    { id: 2, title: "一套框架多种平台 移动端&桌面端", author: "威哥", date: "2022-08-15", imgUrl: "/images/banner3.png", content: reactContent }
  ]

  data.map(val => {
    let sql = `INSERT INTO article VALUES (${val.id}, '${val.title}', '${val.author}', '${val.date}', '${val.imgUrl}', '${val.content}')`
    db.query(sql, (err, data) => {
      if (err) console.log(err)
      console.log(data)
    })
  })
}
fn()

// 注意运行命令不成功原因： 外键约束